package com.jsj.controller;

import com.jsj.pojo.Device;
import com.jsj.pojo.Type;
import com.jsj.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DeviceTypeController {

    @Autowired
    DeviceService deviceService;
//默认查询所有的类型
    @RequestMapping("/queryAllType")
    @ResponseBody
    public List<Type> queryAllType(){
        List<Type> typesList = deviceService.queryAllType();
        System.out.println("---queryAllType--"+typesList);
        return typesList;
    }
//设备类型关键字搜索
    @RequestMapping("/queryType")
    @ResponseBody
    public List<Type> queryType( String type){
        List<Type> typesList = deviceService.queryType(type);
        System.out.println("---queryByType---"+typesList);
        return typesList;
    }
    //点击具体设备类型，查看该类型下所有设备
    @RequestMapping("/queryListByType")
    @ResponseBody
    public List<Device> queryListByType(String type){
        List<Device> devicesList = deviceService.queryListByType(type);

        System.out.println("---queryByType---"+devicesList);
        return devicesList;
    }
//添加设备类型
    @RequestMapping("/addType")
    @ResponseBody
    public Boolean addType(Type type){

        if (type==null||type.getType().isEmpty()){

            return false;
        }
        //判断不能有相同得名称的类型
        List<Type> typeList = deviceService.queryAllType();
        for (Type types : typeList) {
            if (types.getType() == type.getType()) {

                return false;
            }
        }
        //插入非重复的类型
        Boolean flag = deviceService.addType(type);
        return flag;
    }

    @RequestMapping("/delTypeById")
    @ResponseBody
    public Boolean delTypeById(@RequestParam("typeId") int id){
        Boolean flag = deviceService.delTypeById(id);
        return flag;
    }

    @RequestMapping("/updateById")
    @ResponseBody
    public Boolean updateById(Type type){
        Boolean flag = deviceService.updateById(type);
        return flag;
    }
}
